package com.palnar.ilecker.manager;

import java.util.ArrayList;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import com.palnar.ilecker.constants.IServiceConstants;
import com.palnar.ilecker.model.ServiceManagerModel;
import com.palnar.ilecker.utils.Utils;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class AllServiceManagerWithOutArgs extends
		AsyncTask<Void, Void, ArrayList<SoapObject>> implements
		IServiceConstants {
	SoapWithOutArgsListener dataDownloadListener;

	private ArrayList<String> wsdlString;

	private ArrayList<String> soapActionForFunction;
	private ArrayList<String> method;
	private Context context;
	private CustomProgressDialog progress;

	private ArrayList<SoapObject> soapObject;
	private SoapObject soapObjectResponse;

	private boolean isException = false;
	private String exceptionMsg = "";

	public AllServiceManagerWithOutArgs(ArrayList<String> wsdlString,
			ArrayList<String> method, ArrayList<String> soapActionForFunction,
			CustomProgressDialog progress) {

		this.wsdlString = wsdlString;

		this.method = method;
		this.soapActionForFunction = soapActionForFunction;
		this.progress = progress;

	}

	public AllServiceManagerWithOutArgs(
			ArrayList<ServiceManagerModel> managerModel,
			CustomProgressDialog progress) {

		this.progress = progress;

		this.wsdlString=new ArrayList<String>();
		this.method=new ArrayList<String>();
		this.soapActionForFunction=new ArrayList<String>();
		
	//	System.out.println(""+managerModel.size()+"  "+managerModel.get(0).getWsdlString());
		//for (int i = 0; i < managerModel.size(); i++) {
		for (ServiceManagerModel serviceManagerModel : managerModel) {
				
			
			

			this.wsdlString.add(serviceManagerModel.getWsdlString().toString());

			this.method.add(serviceManagerModel.getMethod().toString());
			this.soapActionForFunction.add(serviceManagerModel.getSoapActionForFunction().toString());

		}

	}

	public void setAllServiceWithOutListener(
			SoapWithOutArgsListener dataDownloadListener) {
		this.dataDownloadListener = dataDownloadListener;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		progress.show();

		soapObject = new ArrayList<SoapObject>();

		for (int i = 0; i < wsdlString.size(); i++) {

			Log.e("TEST", "********* " + i + " *************");
			Log.e("TEST", "**********START*************");
			Log.e("TEST", " URL= " + wsdlString);
			Log.e("TEST", " METHOD= " + method);
			Log.e("TEST", " ACTION= " + soapActionForFunction);
			Log.e("TEST", "***********END************");

			SoapObject soapObj = new SoapObject(NAMESPACE, method.get(i));

			soapObject.add(soapObj);

		}

		System.out.println("PRE...");

	}

	@Override
	protected ArrayList<SoapObject> doInBackground(Void... params) {

		ArrayList<SoapObject> soapObjects = new ArrayList<SoapObject>();

		for (int i = 0; i < wsdlString.size(); i++) {

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);

			envelope.dotNet = true;

			envelope.setOutputSoapObject(soapObject.get(i));

			AndroidHttpTransport httpTransport = new AndroidHttpTransport(
					wsdlString.get(i));

			httpTransport.debug = true;

			try {
				System.setProperty("http.keepAlive", "false");

				httpTransport.call(soapActionForFunction.get(i), envelope);

			} catch (Exception e) {
				isException = true;
				exceptionMsg = i + "th one error";

			}

			try {

				soapObjectResponse = (SoapObject) envelope.getResponse();

				soapObjects.add(soapObjectResponse);
			} catch (SoapFault e) {
				e.printStackTrace();

				isException = true;
				exceptionMsg = i + "th one error";
			} catch (Exception e) {
				isException = true;
				exceptionMsg = i + "th one error";

			}

		}

		return soapObjects;
	}

	@Override
	protected void onPostExecute(ArrayList<SoapObject> result) {
		super.onPostExecute(result);
		progress.dismiss();

		System.out.println("POST...");
		if (isException) {

			dataDownloadListener.dataDownloadedFailed(exceptionMsg);
		} else {
			dataDownloadListener.dataDownloadedSuccessfully(result, method);

			Utils.longInfo(result.toString());

		}

	}

	public static interface SoapWithOutArgsListener {

		void dataDownloadedSuccessfully(ArrayList<SoapObject> result,
				ArrayList<String> method);

		void dataDownloadedFailed(String error);

	}

}
